package com.student.dao.impl;
//@Classname AdminInfoDaoImpl
//@time 20:42
//OMEN

import com.student.dao.AdminInfoDao;
import com.student.entity.AdminInfo;
import com.student.util.DBUtil;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminInfoDaoImpl implements AdminInfoDao {
    @Override
    public int insert(AdminInfo entity) throws Exception {
        String sql="insert  into admin_info(admin_id,admin_name,admin_password)values (?,?,?)";
        try(
                Connection con= DBUtil.getConnection();
                PreparedStatement pstm=con.prepareStatement(sql);
                ) {
            pstm.setObject(1,entity.getAdminId());
            pstm.setObject(2,entity.getAdminName());
            pstm.setObject(3,entity.getAdminPassword());
            return pstm.executeUpdate();

        }


    }

    @Override
    public int updateById(AdminInfo entity) throws Exception {
        String sql="update admininfo set admin_name=?,admin_password=? where admin_id=?";
        try (
                Connection con=DBUtil.getConnection();
                PreparedStatement pstm=con.prepareStatement(sql);

                ){
            pstm.setObject(1,entity.getAdminName());
            pstm.setObject(2,entity.getAdminPassword());
            pstm.setObject(3,entity.getAdminId());
            return pstm.executeUpdate();


        }
    }

    @Override
    public int deleteById(Serializable id) throws Exception {
        String sql ="delete from admininfo where admin_id=?";
        try(
                Connection con=DBUtil.getConnection();
                PreparedStatement pstm=con.prepareStatement(sql);

                ) {
            pstm.setObject(1,id);
            return pstm.executeUpdate();
        }

    }

    @Override
    public AdminInfo selectById(Serializable id) throws SQLException {
        String sql ="select * from admin_info where admin_id=?";
        Connection con =null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
            con=DBUtil.getConnection();
            pstm=con.prepareStatement(sql);
            pstm.setObject(1,id);
            rs=pstm.executeQuery();
            if (rs.next()){
                AdminInfo af=new AdminInfo();
                af.setAdminId(rs.getInt("admin_id"));
                af.setAdminName(rs.getString("admin_name"));
                af.setAdminPassword(rs.getString("admin_password"));


                return af;

            }else {
                throw new SQLException("没有数据");
            }


        }finally {
            DBUtil.close(con,pstm,rs);

        }
    }

    @Override
    public List<AdminInfo> select(int index, int len) throws SQLException {
        String sql ="select * from admin_info order by admin_id limit ?,?";
        Connection con=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
            con=DBUtil.getConnection();
            pstm=con.prepareStatement(sql);
            pstm.setObject(1,index);
            pstm.setObject(2,len);
            rs=pstm.executeQuery();
            List<AdminInfo> list=new ArrayList<>();
            while (rs.next()){
                AdminInfo ad=new AdminInfo();
                ad.setAdminId(rs.getInt("admin_id"));
                ad.setAdminName(rs.getString("admin_name"));
                ad.setAdminPassword(rs.getString("admin_password"));
                list.add(ad);
            }
            return list;

        }finally {
            DBUtil.close(con,pstm,rs);
        }


    }

    @Override
    public long count() throws SQLException {
        return 0;
    }
}
